package com.pisolution.actuator.client.dependencies;

import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * 用于服务端索取客户端应用第三方依赖.
 *
 * @author sora
 * @version 0.1.0
 */
@SuppressWarnings("unused")
@RestControllerEndpoint(id = "dependencies")
public class DependenciesEndpoint {
    /** 应用依赖检测工具. */
    private final DependencyDetector detector;

    // -------------------------------------------------------------------------------------------------
    //  初始化
    // -------------------------------------------------------------------------------------------------

    public DependenciesEndpoint() {
        this.detector = new DependencyDetector();
    }

    // -------------------------------------------------------------------------------------------------
    //  应用依赖检测
    // -------------------------------------------------------------------------------------------------

    /**
     * 检测应用依赖信息并予以反馈.
     *
     * @return 应用依赖信息.
     */
    @RequestMapping(method = RequestMethod.GET)
    public List<ApplicationDependency> detect() {
        // 1. 如果尚未检测过 则进行依赖检测.
        if (this.detector.getDependencyList().isEmpty()) {
            this.detector.detect();
        }

        // 2. 反馈检测到的应用依赖信息.
        return this.detector.getDependencyList();
    }
}
